<?php

class Kieumau_Controller_Admin extends Kieumau_Controller_MainController//Zend_Controller_Action
{
	protected 	$fieldsValidate = array();
	
	public function init(){
		
		parent::init();
		try{
			$dirTemplate = TEMPLATE_PATH;
			$sectionConfig = 'template';
			$fileConfig = 'default/template.ini';
			$this->createTemplate($dirTemplate, $sectionConfig, $fileConfig );
		
		}catch(Exception $ex){
			echo "error: ".$ex->getMessage();
			die;
		}
	}
	
	
	// @author: Michael.Tran
	// @date: 11/07/2012
	// @function: createtemplate -> Tạo template cho controller
	public function createTemplate($dirTemplate, $sectionConfig = 'template', $fileConfig = 'template.ini' ){
	
		try{
			$module =$this->getRequest()->getModuleName();
			$filename = $dirTemplate . '/'.$module.'/' . $fileConfig;
	
			$section = $sectionConfig;
			$config = new Zend_Config_Ini($filename, $section);
			$config = $config->toArray();
		  
			$baseUrl = $this->_request->getBaseUrl();
			$dirTemplateUrl = $baseUrl .'/public/'. $config['url'];
	
	
	
			// 	    	echo "<br/>".$this->view->dirCss = $dirTemplateUrl . $config['dirCss'];
			// 	    	echo "<br/>".$this->view->dirImg = $dirTemplateUrl . $config['dirImg'];
			// 	    	echo "<br/>".$this->view->dirJs = $dirTemplateUrl . $config['dirJs'];
	
			$dirCss 			= $dirTemplateUrl . $config['dirCss'];
			$dirJs 				= $dirTemplateUrl . $config['dirJs'];
	
	
			$this->view->dirCss = $dirTemplateUrl . $config['dirCss'];
			$this->view->dirImg = $dirTemplateUrl . $config['dirImg'];
			$this->view->dirJs 	= $dirTemplateUrl . $config['dirJs'];
	
	
			$option = array('layout'=> $config['layout'],
					'layoutPath'=>PUBLIC_PATH.'/'.$config['url']);
			//'layoutPath'=>PUBLIC_PATH.'/templates/front/default');
			Zend_Layout::startMvc($option);
	
			// 	    	$layoutPath = PUBLIC_PATH . '/templates/admin/default';
			// 	    	$options = array ('layout' => 'index', 'layoutPath' => $layoutPath );
			// 	    	Zend_Layout::startMvc ( $options );
	
		}catch(Exception $ex){
			echo "<br/> Error: ".$ex->getMessage();
			exit;
		}
	}
	
	
	// @author: Peter.Nguyen
	// @date: 5/24/2012
	// @function: validate fields automatically
	// @return: array 
	public function fieldsValidate($dataForm){
		$errorList = array();
		if(count($this->fieldsValidate)>0){
			foreach($this->fieldsValidate as $field => $function){
				$dataField = $dataForm[$field];
				$error = $this->$function($dataField);				
				if(!empty($error)){
					$errorList[]= $error;	
				}
			}
			
		}
		return $errorList;
	}
	
	// @author: Peter.Nguyen
	// $date: 5/24/2012
	// @function: check data is name
	// @return: String 
	public function isName($data){	
		
		if(isset($data['en']) && $data['en'] == ""){
			return $this->language->l('nameCantEmpty');	
		}
		return "";
		
	}
	
	// @author: Peter.Nguyen
	// $date: 5/24/2012
	// @function: check data is name
	// @return: String 
	public function isDescription($data){
		if(isset($data['en']) && $data['en'] == ""){
			return $this->language->l('descriptionCantEmpty');	
		}
		return "";
	}
	
	public function isEmailValid($email){
		$validator = new Zend_Validate_EmailAddress();
		 if (!$validator->isValid($email))
			return $this->language->l('emailNoValid');
		 return "";
	}
	
	public function isEmailExists($email){
		$validator = new Zend_Validate_Db_NoRecordExists(
			array(
				'table' => 'users',
				'field' => 'email',
				'exclude' => array(//Không xét những dòng sau:
					'field' => 'id',
					'value' => $user_info->id
				)
			)
		);
		
		if (!($validator->isValid($email))) {//Đã có trong database
			$result ['result'] ['code'] = 'NOK';
			$result ['result'] ['title'] = 'Waring!';
			$result ['result'] ['msg'] = $this->language->l("emailAlreadyUse");
			return $result;
		}
		else{//Chua có trong database
		
		}
	}
	
	
	// @author: Michael.Tran
	// $date: 07/22/2012
	// @function: validate form automatically
	// @return: array
	public function formValidate($formData,$validateParams = array(),$replace = array()){
		$errorList = array();
		if(count($validateParams)>0){
			foreach($validateParams as $field => $function){
				$error = @$this->$function($formData[$field], $field);
				if(!empty($error)){
					$errorList[]= $error;
				}
			}
				
		}
		return $errorList;
	}
	
	
	// @author: Michael.Tran
	// $date: 07/22/2012
	// @function: check data is name
	// @return: String
	public function setRequired($data, $name){
		$flanguage = $this->defaultLanguage;
		if(isset($data[$flanguage]) && $data[$flanguage] == ""){
			return $name.' '.$this->language->l('CantEmpty');
		}
		return "";
	}
	
	// @author: Michael.Tran
	// $date: 08/08/2012
	// @function: check data is name
	// @return: String
	public function setSelectRequired($data, $name){
		
		$flanguage = $this->defaultLanguage;
		if(empty($data)){
			return $this->language->l('Please select a') . ' ' . $name;
		}
		return "";
	}
	
	
	// @author: Michael
	// $date: 5/24/2012
	// @function: create template for admin
	// @return: string
	/*
    public function createTemplate($dirTemplate, $sectionConfig = 'template', $fileConfig = 'template.ini' ){
    
    try{
	    	$module =$this->getRequest()->getModuleName();
	    	$filename = $dirTemplate . '/'.$module.'/' . $fileConfig;
	
	    	$section = $sectionConfig;
	    	$config = new Zend_Config_Ini($filename, $section);
	    	$config = $config->toArray();
	    
	    	$baseUrl = $this->_request->getBaseUrl();
	    	$dirTemplateUrl = $baseUrl .'/public/'. $config['url'];
	    	
	    	$dirJs = $dirTemplateUrl . $config['dirJs'];
	
	    	$this->view->dirCss = $dirTemplateUrl . $config['dirCss'];
	    	$this->view->dirImg = $dirTemplateUrl . $config['dirImg'];
	    	$this->view->dirJs = $dirTemplateUrl . $config['dirJs'];
	    	
	    	
	    	$option = array('layout'=> $config['layout'],
	    			'layoutPath'=>PUBLIC_PATH.'/'.$config['url']);
	    			//'layoutPath'=>PUBLIC_PATH.'/templates/front/default');
	    	Zend_Layout::startMvc($option);
	    	
    	}catch(Exception $ex){
    		echo "<br/> Error: ".$ex->getMessage();
    		exit;
    	}
    }
    
    /**/
}
